package com.milhouz.wormdroid.gl.model.hud;

import javax.microedition.khronos.opengles.GL10;

import android.graphics.Paint;
import android.os.SystemClock;

import com.milhouz.wormdroid.gl.model.text.TextSprite;

public class TimeHudSprite extends TextSprite {

	public long totalTime;
	public long startTime;
	public long currentTime;
	
	public TimeHudSprite(int roundTime, float x, float y, Paint paint) {
		super(HudSpriteNames.TIME_SPRITE, x, y, paint);
		totalTime = roundTime*1000;
	}

	private void resetAndStart() {
		startTime = SystemClock.elapsedRealtime();
		currentTime = totalTime;
	}

	public boolean isEndOfRound() {
		if (currentTime <= 0) {
			resetAndStart();
			return true;
		}
		return false;
	}
	
	@Override
	public void initialize(GL10 gl) {
		resetAndStart();
		super.initialize(gl);
	}

	@Override
	public void draw(GL10 gl, float viewWidth, float viewHeight) {
		if (currentTime > 0)
			currentTime = totalTime - (SystemClock.elapsedRealtime() - startTime);
		super.setText(getFormatedTime());
		super.draw(gl, viewWidth, viewHeight);
	}
	
	private String getFormatedTime() {
		return String.valueOf((int)(currentTime/1000));
	}
	
}
